package biz.pokrovskoe.reprox

class ReportServerController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [reportServerInstanceList: ReportServer.list(params), reportServerInstanceTotal: ReportServer.count()]
    }

    def create = {
        def reportServerInstance = new ReportServer()
        reportServerInstance.properties = params
        return [reportServerInstance: reportServerInstance]
    }

    def save = {
        def reportServerInstance = new ReportServer(params)
        if (reportServerInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), reportServerInstance.id])}"
            redirect(action: "show", id: reportServerInstance.id)
        }
        else {
            render(view: "create", model: [reportServerInstance: reportServerInstance])
        }
    }

    def show = {
        def reportServerInstance = ReportServer.get(params.id)
        if (!reportServerInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), params.id])}"
            redirect(action: "list")
        }
        else {
            [reportServerInstance: reportServerInstance]
        }
    }

    def edit = {
        def reportServerInstance = ReportServer.get(params.id)
        if (!reportServerInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [reportServerInstance: reportServerInstance]
        }
    }

    def update = {
        def reportServerInstance = ReportServer.get(params.id)
        if (reportServerInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (reportServerInstance.version > version) {
                    
                    reportServerInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'reportServer.label', default: 'ReportServer')] as Object[], "Another user has updated this ReportServer while you were editing")
                    render(view: "edit", model: [reportServerInstance: reportServerInstance])
                    return
                }
            }
            reportServerInstance.properties = params
            if (!reportServerInstance.hasErrors() && reportServerInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), reportServerInstance.id])}"
                redirect(action: "show", id: reportServerInstance.id)
            }
            else {
                render(view: "edit", model: [reportServerInstance: reportServerInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def reportServerInstance = ReportServer.get(params.id)
        if (reportServerInstance) {
            try {
                reportServerInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'reportServer.label', default: 'ReportServer'), params.id])}"
            redirect(action: "list")
        }
    }
}
